package com.laxcen.toydemo;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.annotation.Nullable;

import com.laxcen.controls.HomeMenuButtonControl;
import com.laxcen.controls.VideoItemControl;
import com.laxcen.intelligenttoy.BLE.ETag;

import util.BackgroundMusic;
import util.DelayHandler;

public class LearningActivity extends BaseActivity implements View.OnClickListener {
    GameState mGameState = GameState.instance(this);
    ImageButton btn_menu_course = null;
    VideoView videoPlayer = null;
    ImageView imgVideoPoster = null;
    HomeMenuButtonControl btn_home, btn_play, btn_pasue = null;
    VideoItemControl video_item1, video_item2, video_item3, video_item4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_learning);

        video_item1 = findViewById(R.id.video_item1);
        video_item2 = findViewById(R.id.video_item2);
        video_item3 = findViewById(R.id.video_item3);
        video_item4 = findViewById(R.id.video_item4);
        video_item1.setOnClickListener(this);
        video_item2.setOnClickListener(this);
        video_item3.setOnClickListener(this);
        video_item4.setOnClickListener(this);

        btn_home = findViewById(R.id.btn_home);
        btn_home.setOnClickListener(this);


    }

    private void startRepeatPromptGbSound() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.Learning) {
                    BackgroundMusic.instance(LearningActivity.this).play(R.raw.learning_firstopen_repeat_prompt, false);
                }
            }
        }, 1000);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_home) {
            startActivityAndFinish(GameActivity.class);
        } else if (v.getId() == R.id.video_item1 || v.getId() == R.id.video_item2 || v.getId() == R.id.video_item3 || v.getId() == R.id.video_item4) {
            startActivity(VideoPlayerActivity.class);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGameState.setStep(GameState.EGameStep.Learning);
        restartTagCheck();
    }

    @Override
    protected void onPause() {
        super.onPause();
        isWaitTag = false;
        BackgroundMusic.instance(this).stop();
    }


    private boolean isWaitTag = true;

    private void restartTagCheck() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                isWaitTag = true;
                startTagCheck();
            }
        }, 1000);
    }

    /* 7,8,9 = 1,2,3 */
    private void startTagCheck() {
        if (!isWaitTag) return;
        if (checkOption(currentTags, ETag._1)) {
            isWaitTag = false;
            video_item1.shake();
            openVideoPlayer();
        } else if (checkOption(currentTags, ETag._2)) {
            isWaitTag = false;
            video_item2.shake();
            openVideoPlayer();
        } else if (checkOption(currentTags, ETag._3)) {
            isWaitTag = false;
            video_item3.shake();
            openVideoPlayer();
        } else if (checkOption(currentTags, ETag._4)) {
            isWaitTag = false;
            video_item4.shake();
            openVideoPlayer();
        } else if (checkOption(currentTags, ETag._GOBACK)) {
            isWaitTag = false;
            startActivityAndFinish(GameActivity.class);
        }

        if (isWaitTag) {
            DelayHandler.newHandler().delay(new Runnable() {
                @Override
                public void run() {
                    startTagCheck();
                }
            }, LoopDetectionMillis);
        }
    }

    private void openVideoPlayer() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                startActivity(VideoPlayerActivity.class);
            }
        }, 1000);
    }
}
